package com.example.leetcode.leetcode.tree;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {

    }

    public TreeNode(int x) {
        val = x;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public static void preorder(TreeNode root) {
        if (null == root) {
            return;
        }
        System.out.print(root.val);
        if (null != root.left) {
            preorder(root.left);
        }
        if (null != root.right) {
            preorder(root.right);
        }
    }

    // 中序遍历
    public static void medium(TreeNode root) {
        if (null == root) {
            return;
        }
        if (null != root.left) {
            medium(root.left);
        }
        System.out.print(root.val);
        if (null != root.right) {
            medium(root.right);
        }
    }

    // 后续遍历
    public static void subsequent(TreeNode root) {
        if (null == root) {
            return;
        }
        if (null != root.left) {
            subsequent(root.left);
        }
        if (null != root.right) {
            subsequent(root.right);
        }
        System.out.print(root.val);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode rl = new TreeNode(2);
        TreeNode rr = new TreeNode(3);
        root.left = rl;
        root.right = rr;
        subsequent(root);
    }
}